/*
 * Copyright 2017-2101 Innel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hykj.xxltest.controller;

import com.github.pagehelper.PageInfo;
import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.base.RequiresRole;
import com.hykj.xxltest.model.CourierModel;
import com.hykj.xxltest.model.UserTxModel;
import com.hykj.xxltest.service.CourierService;
import com.hykj.xxltest.service.UserTxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author innel
 * @date 2018-07-11 16:40:52
 */
@Controller
@RequestMapping("/usertx")
@Api(description = "")
public class UserTxController {

    private static final Logger logger = Logger.getLogger(UserTxController.class);

    @Autowired
    private UserTxService userTxService;
    @Autowired
    private CourierService courierService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取列表",
            notes = "备注：",
            response = UserTxModel.class,
            httpMethod = "POST")
    public AppBack list(@ApiParam(value = "每页显示个数,不填默认10")
                        @RequestParam(required = false)
                                Integer pageSize,
                        @ApiParam(value = "userid")
                        @RequestParam(required = false)
                                Integer userid,
                        @ApiParam(value = "用户类型 1=用户 2=工人")
                        @RequestParam(required = false)
                                Integer usertype,
                        @ApiParam(value = "0 = 待审核 1=审核通过 -1 =审核失败")
                        @RequestParam(required = false)
                                Integer status,
                        @ApiParam(value = "开始时间 范例 2017-11-06 16:02:11", hidden = true)
                        @RequestParam(required = false)
                                String startTime,
                        @ApiParam(value = "截止时间 格式 YYYYMMDD 也可以", hidden = true)
                        @RequestParam(required = false)
                                String endTime,
                        @ApiParam(value = "搜索关键字")
                        @RequestParam(required = false)
                                String searchKey,
                        @ApiParam(value = "是否是后台列表请求", hidden = true)
                        @RequestParam(required = false)
                                String backList,
                        @ApiParam(value = "是否简单显示", hidden = true)
                        @RequestParam(required = false)
                                String noD,
                        @ApiParam(value = "不填不分页")
                        @RequestParam(required = false)
                                Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageSize", pageSize);
        params.put("userid", userid);
        params.put("usertype", usertype);
        params.put("status", status);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("searchKey", searchKey);
        params.put("noD", noD);
        params.put("backList", backList);
        if (page == null) {
            return new AppBack(userTxService.getDList(params));
        }
        PageInfo<UserTxModel> p = userTxService.getDList(params, page);
        return new AppBack(p);
    }
/**
 * 提现
 */
    @RequestMapping("/tx")
    @ResponseBody
    @ApiOperation(
            value = "获取",
            notes = "备注：",
            httpMethod = "POST"
    )
    public AppBack tx(Integer userid,String account,String name,BigDecimal amount){

//         if (amount <= 0){
//             return new AppBack(-1,"金额不对");
//         }
          CourierModel courierModel = courierService.getById(userid);
         if (courierModel == null){
             return new AppBack(-2,"用户不存在");
         }
         if (amount.compareTo(courierModel.getAmount()) == 1){
             return new AppBack(-3,"余额不足");
         }
         UserTxModel userTxModel = new UserTxModel();
         userTxModel.setStatus(0);
         userTxModel.setCreateTime(new Date());
         userTxModel.setAccount(account);
         userTxModel.setAccountName(name);
         userTxModel.setUserid(userid);
         userTxModel.setAmount(amount);
         userTxService.create(userTxModel);

         courierModel.setAmount(courierModel.getBzamount().subtract(amount));
         courierService.update(courierModel);
        return new AppBack();
    }
    /**
     * 信息
     */
    @RequestMapping("/info")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "获取",
            notes = "备注：",
            response = UserTxModel.class,
            httpMethod = "POST")
    public AppBack info(
            @ApiParam(value = "的ID", required = true)
            @RequestParam Integer userTxId) {
        UserTxModel userTx = userTxService.getByIdD(userTxId);
        if (userTx == null) {
            return new AppBack(-1, "当前资源不存在");
        }
        return new AppBack(userTx);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
//    @RequiresRole(role = "1")
    @ApiOperation(
            value = "保存",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack save(
            @ApiParam(value = "userid", required = true)
            @RequestParam
                    Integer userid,


            @ApiParam(value = "用户类型 1=用户 2=工人", required = true)
            @RequestParam
                    Integer usertype,


            @ApiParam(value = "提现金额", required = true)
            @RequestParam
                    BigDecimal amount,


            @ApiParam(value = "剩余金额", required = true)
            @RequestParam
                    BigDecimal remainAmount,


            @ApiParam(value = "0 = 待审核 1=审核通过 -1 =审核失败", required = true)
            @RequestParam
                    Integer status,


            @ApiParam(value = "审核时间", required = true)
            @RequestParam
                    Date passTime,


            @ApiParam(value = "提现账号", required = true)
            @RequestParam
                    String account,


            @ApiParam(value = "账户姓名", required = true)
            @RequestParam
                    String accountName,


            @ApiParam(value = "提现平台", required = true)
            @RequestParam
                    String accountPt,

            @ApiParam(value = "选填参数控制 默认不填", hidden = true)
            @RequestParam(required = false)
                    Integer extra) {
        UserTxModel userTx = new UserTxModel();

        userTx.setUserid(userid);


        userTx.setUsertype(usertype);


        userTx.setAmount(amount);


        userTx.setRemainAmount(remainAmount);


        userTx.setStatus(0);


        userTx.setPassTime(new Date());


        userTx.setAccount(account);


        userTx.setAccountName(accountName);


        userTx.setAccountPt(accountPt);


        userTxService.create(userTx);
        return new AppBack();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "更新",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack update(@ApiParam(value = "userid")
                          @RequestParam(required = false)
                                  Integer userid,
                          @ApiParam(value = "用户类型 1=用户 2=工人")
                          @RequestParam(required = false)
                                  Integer usertype,
                          @ApiParam(value = "提现金额")
                          @RequestParam(required = false)
                                  BigDecimal amount,
                          @ApiParam(value = "剩余金额")
                          @RequestParam(required = false)
                                  BigDecimal remainAmount,
                          @ApiParam(value = "0 = 待审核 1=审核通过 -1 =审核失败")
                          @RequestParam(required = false)
                                  Integer status,
                          @ApiParam(value = "审核时间")
                          @RequestParam(required = false)
                                  Date passTime,
                          @ApiParam(value = "提现账号")
                          @RequestParam(required = false)
                                  String account,
                          @ApiParam(value = "账户姓名")
                          @RequestParam(required = false)
                                  String accountName,
                          @ApiParam(value = "提现平台")
                          @RequestParam(required = false)
                                  String accountPt,
                          @ApiParam(value = "的ID", required = true)
                          @RequestParam int userTxId) {
        UserTxModel userTx = new UserTxModel();
        userTx.setUserid(userid);
        userTx.setUsertype(usertype);
        userTx.setAmount(amount);
        userTx.setRemainAmount(remainAmount);
        userTx.setStatus(status);
        userTx.setPassTime(passTime);
        userTx.setAccount(account);
        userTx.setAccountName(accountName);
        userTx.setAccountPt(accountPt);
        userTx.setId(userTxId);

        userTxService.update(userTx);
        return new AppBack();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresRole(role = "1")
    @ApiOperation(
            value = "删除",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack delete(@ApiParam(value = "的ID", required = true)
                          @RequestParam
                                  int userTxId) {

        UserTxModel newUserTx = new UserTxModel();
        newUserTx.setStatus(-10);
        newUserTx.setId(userTxId);
        userTxService.update(newUserTx);
        return new AppBack();
    }

}
